package com.cskaoyan.lz_service;

import com.cskaoyan.bean.OrderExample;
import com.cskaoyan.bean.User;


import com.cskaoyan.bean.UserRows;
import com.cskaoyan.bean.UserStatBean;
import com.cskaoyan.bean.lzp.Order;
import com.cskaoyan.bean.lzp.UserWxIndexVO;
import com.cskaoyan.bean.lzpackage.UserData;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public UserData query(BaseParam param, String username,String mobile) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<User> list = userMapper.select(sort,order,username,mobile);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<User> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return UserData.data(total,pages,limit,page,list);
    }

    @Override
    public UserStatBean statUser() {
        //查询数据表-user
        List<UserRows> rows = userMapper.statUser();
        UserStatBean userStatBean = new UserStatBean();
        List<String> columns = new ArrayList<>();
        columns.add(0,"day");
        columns.add(1,"users");
        userStatBean.setColumns(columns);
        userStatBean.setRows(rows);
        return userStatBean;
    }

    @Override
    public UserWxIndexVO userWxIndex(Integer id) {
        Order order = new Order();
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andOrderStatusEqualTo((short) 101);
        long unpaid = orderMapper.countByExample(example);
        order.setUnpaid(unpaid);

        //待评价个数----根据用户的id查询order-goods表，，goods没有评价的商品
        OrderExample example1 = new OrderExample();
        OrderExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUserIdEqualTo(id);
        criteria1.andOrderStatusEqualTo((short) 401);
        long uncomment = orderMapper.countByExample(example1);
        order.setUncomment(uncomment);

        OrderExample example2 = new OrderExample();
        OrderExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andUserIdEqualTo(id);
        criteria2.andOrderStatusEqualTo((short) 301);
        long unrecv = orderMapper.countByExample(example2);
        order.setUnrecv(unrecv);

        OrderExample example3 = new OrderExample();
        OrderExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andUserIdEqualTo(id);
        criteria3.andOrderStatusEqualTo((short) 201);
        long unship = orderMapper.countByExample(example);
        order.setUnship(unship);

        UserWxIndexVO userWxIndexVO = new UserWxIndexVO();
        userWxIndexVO.setOrder(order);
        return userWxIndexVO;
    }
}
